package test.webface;

import net.sourceforge.jwebunit.junit.WebTester;

import org.junit.*;
import org.eclipse.jetty.server.*;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.webapp.WebAppContext;

public class LoginPage {
	private static WebTester tester;
	private static Server server;

	@BeforeClass
	public static void init() throws Exception{
		tester = new WebTester();
		server = new Server(8082);
		
		//server.addHandler(new WebAppContext("", ""));
		ContextHandler context1 = new ContextHandler();
		final String secondPart = "controller";
		
		//context1.setContextPath("/WebContent");
		Handler handler1 = new WebAppContext("WebContent/WEB-INF", "/"+secondPart);
		context1.setHandler(handler1);
		//ContextHandlerCollection contexts = new ContextHandlerCollection();
		//contexts.setHandlers(new Handler[] { context1 });
		server.setHandler(context1);
		server.stop();
		server.start();
		
		int port = ((ServerConnector)server.getConnectors()[0]).getLocalPort();
		
		System.out.println(port);
		server.join();
		
		tester.getTestContext().setBaseUrl("http://localhost:" + port + "/"+secondPart);
	}
	
	@Test
	public void testLogin(){
		tester.beginAt("/login.jsp");
		tester.assertFormPresent("form_login");
		tester.assertFormElementPresent("username");
		tester.assertFormElementPresent("password");
		tester.assertButtonPresentWithText("Login");
	}
	
	@AfterClass
	public static void end() throws Exception {
		server.stop();
	}
}
